package com.gmrz.uap.facet;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gmrz.uap.model.Facets;
import com.jfinal.aop.Clear;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class FacetsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Clear
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        JSONObject jsonObject = new JSONObject();//总结点

        JSONObject trustedFacetsJsonObject = new JSONObject();//trustedFacets节点

        JSONObject versionJsonObject = new JSONObject();//编辑version节点
        versionJsonObject.put("major", 1);
        versionJsonObject.put("minor", 0);

        JSONArray idsJsonArray = new JSONArray();//ids节点
        List<Facets> facetsList = FacetService.me.findAll();

        for (Facets facets : facetsList) {
            idsJsonArray.add(facets.getFacetName());
        }

        trustedFacetsJsonObject.put("version", versionJsonObject);
        trustedFacetsJsonObject.put("ids", idsJsonArray);

        jsonObject.put("trustedFacets", trustedFacetsJsonObject);

        response.getOutputStream().write(jsonObject.toJSONString().getBytes());
    }
}
